<template>
  <el-container class="home-container">
    <el-header>
      <div class="logoBox">
        <img src="../assets/heima.png" alt="">
        <span class="title">电商后台管理系统</span>
      </div>
      <div>
        <span class="user">当前用户：{{username}}</span>
        <el-button type="info" @click="logout">退出</el-button>
      </div>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse? '64px' :'200px' ">
        <div @click="toggleCollapse" class="toggle_collapse">|||</div>
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#409eff" unique-opened router :default-active="activePath" :collapse="isCollapse" :collapse-transition="false">
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单 -->
            <template slot="title">
              <i :class="icons[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <template>
              <!-- 二级菜单 -->
              <el-menu-item :index="'/'+ subItem.path" @click="saveActivePath('/'+ subItem.path)" v-for="subItem in item.children" :key="subItem.id">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </el-menu-item>
            </template>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 当前登录用户名
      username: '',
      // 菜单数据
      menuList: [],
      // 需要渲染的icon图标
      icons: {
        '125': 'iconfont icon-users',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      // 默认激活的菜单
      activePath: '',
      // 是否需要折叠
      isCollapse: false
    }
  },
  methods: {
    // 退出功能
    async logout() {
      const confirmResult = await this.$confirm('确定退出吗', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(error => error);
      if (confirmResult === 'confirm') {
        this.$router.push('/login');
        // 清楚本地数据
        window.sessionStorage.clear();
      }
    },

    // 获取接口左侧菜单数据
    async getMenuList() {
      const res = await this.$axios.get('menus');
      if (res.meta.status !== 200) return this.$message.error('获取菜单列表数据失败！');
      this.menuList = res.data;
    },
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    // 保存激活的菜单路径
    saveActivePath(activePath) {
      window.sessionStorage.setItem('activePath', activePath);
      this.activePath = activePath;
    }
  },
  created() {
    this.getMenuList();
    // 浏览器前进后退按钮 设置 活动路径为当前路由页面的路径
    window.addEventListener('popstate', () => {
      // console.log(this.$route.path);
      this.activePath = this.$route.path;
      window.sessionStorage.setItem('activePath', this.$route.path);
    });
    this.username = window.sessionStorage.getItem('username');
    this.activePath = window.sessionStorage.getItem('activePath');
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.logoBox {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  font-size: 20px;
  font-weight: 500;
  letter-spacing: 0.2em;
  img {
    margin-right: 15px;
  }
}
.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #373d41;
}
.el-aside {
  background-color: #333744;
}
.el-main {
  background-color: #eaedf1;
}
.user {
  margin-right: 15px;
  color: #fff;
}
.el-aside {
  background-color: #333744;
  .el-menu {
    background-color: #333744;
    border-right: 0;
  }
}

.iconfont {
  margin-right: 10px;
}
.toggle_collapse {
  background-color: #4a5064;
  text-align: center;
  line-height: 20px;
  font-size: 12px;
  color: #fff;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>